package com.tien.service.impl;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.tien.dao.UserDao;
import com.tien.model.User;

/**
 * 
 * @author Tian
 * @Mail tianxin_star@163.com
 * @version date：Mar 9, 2012 2:01:47 PM
 * 
 */
@Service
public class UserDetailServiceImpl implements UserDetailsService {
	private static final Log logger = LogFactory
			.getLog(UserDetailServiceImpl.class);
	@Autowired
	private UserDao userDao;

	@Override
	public UserDetails loadUserByUsername(String userName)
			throws UsernameNotFoundException {

		if (userName == null) {
			logger.error("username is null");
		}
		if (userDao == null) {
			logger.error("userDao is null");
		}
		User user = userDao.getUserByUserName(userName);
		// TOTO 判断用户存在，如果不存在，则抛出异常。
		if (user == null) {
			logger.error(userName + " is not exist",
					new UsernameNotFoundException(userName + " is not exist"));
		}

		return user;
	}

}
